package cn.tangwei.product.cotroller;

import cn.tangwei.basic.utils.AjaxRusult;
import cn.tangwei.basic.utils.LoginContext;
import cn.tangwei.basic.utils.PageList;
import cn.tangwei.pet.domain.Pet;
import cn.tangwei.product.domain.Product;
import cn.tangwei.product.query.ProductQuery;
import cn.tangwei.product.service.IProductService;
import cn.tangwei.user.domain.LoginInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *  服务管理接口
 *      写接口的三要素：
 *           1.接口的请求地址是什么；
 *           2.请求参数是什么；
 *           3.返回值类型是什么。
 */
@RestController
@RequestMapping("/product")
@Api(tags = "服务接口",description = "服务接口详细描述")
public class ProductContorller {
    @Autowired
    private IProductService productService;

    /**
     * 新增或修改服务 put
     * 传对象就用@RequestBody
     * @param product
     * @return
     */
    @ApiOperation(value = "服务添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxRusult addOrupdate(@RequestBody Product product, HttpServletRequest request){
        try {
            if(product.getId() == null){
                LoginInfo loginInfo = LoginContext.currentLoginInfo(request);
                productService.add(product, loginInfo);
            }else {
                productService.update(product);
            }
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 删除服务 delete
     * 传值就用@PathVariable("id")再josn里面拿数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxRusult del(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxRusult patchDel(@RequestBody List<Long> ids){
        try {
            productService.patchDelete(ids);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 查询一个服务 get
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product queryById(@PathVariable("id") Long id){
        return productService.queryById(id);
    }

    /**
     * 查询全部服务  get
     * @return
     */
    @GetMapping
    public List<Product> queryAll(){
        return productService.queryAll();
    }

    /**
     * 分页加高级查询 post
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery query){
        return productService.queryPage(query);
    }

    /**
     *  商品上架
     * @param ids
     * @return
     */
    @PatchMapping("/onsale")
    public AjaxRusult onsale(@RequestBody List<Long> ids){
        try {
            productService.onsale(ids);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 商品下架
     * @param ids
     * @return
     */
    @PatchMapping("/offsale")
    public AjaxRusult offsale(@RequestBody List<Long> ids){
        try {
            productService.offsale(ids);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    @GetMapping("/detail/{id}")
    public AjaxRusult detail(@PathVariable("id") Long id){
        try {
            Product product = productService.queryById(id);
            return AjaxRusult.me().setResultObj(product);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }
}
